package com.wash.ctrl.cart;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wash.framework.utils.JsonUtil;
import com.wash.model.Cart;
import com.wash.model.CartItem;
import com.wash.model.Goods;
import com.wash.service.GoodsService;
import com.wash.service.ShoppingCartService;



@Lazy(true)
@Controller
@RequestMapping
public class ShoppingCartCtrl{
	
	private static final String GOODS_PREFEX="gid-";

	@Autowired
	private ShoppingCartService shoppingCartService;
	
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 查看购物车
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping
	@ResponseBody
	public String viewMyCart(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession(true);
		Object userId=session.getAttribute("userId");
		Cart cart=new Cart();
		//用户未登录
		if(userId==null){
			Cookie[] cookies=request.getCookies();
			if(cookies!=null){
				List<CartItem> cartItems=new ArrayList<CartItem>();
				BigDecimal totalPrice=new BigDecimal(0);
				int count=0;
				for(Cookie c : cookies){
					String cookieName=c.getName();
					if(cookieName.startsWith(GOODS_PREFEX)){
						int goodsId=Integer.parseInt(cookieName.split("-")[1]);
						int quantity=Integer.parseInt(c.getValue());
						CartItem cartItem=new CartItem();
						cartItem.setGoodsId(goodsId);
						cartItem.setQuantity(quantity);
						count+=quantity;
						Goods g=goodsService.getEntity(goodsId);
						if(g!=null){
							cartItem.setGoodsName(g.getName());
							cartItem.setPrice(g.getPrice());
							totalPrice=totalPrice.add(g.getPrice().multiply(new BigDecimal(quantity)));
						}
						cartItems.add(cartItem);
					}
				}
				cart.setCount(count);
				cart.setTotalPrice(totalPrice);
				cart.setCartItems(cartItems);
			}
		}else{
			cart=shoppingCartService.getCartInfo(Integer.parseInt(userId.toString()));
		}
		return JsonUtil.writeObject(cart);
	}
	
	/**
	 * 修改购物车里商品数量
	 * @param request
	 * @param response
	 */
	@RequestMapping
	@ResponseBody
	public String changeGnumInCart(HttpServletRequest request,HttpServletResponse response){
		//商品ID
		int goodsId=Integer.parseInt(request.getParameter("goodsId").toString());
		//商品数量
		int quantity=Integer.parseInt(request.getParameter("quantity").toString());
		//1代表增加,-1代表减少
		int flag=Integer.parseInt(request.getParameter("flag").toString());
		HttpSession session=request.getSession(true);
		Object userId=session.getAttribute("userId");
		//用户未登录
		if(userId==null){
			Cookie[] cookies=request.getCookies();
			boolean cflag=false;
			if(cookies!=null){
				for(Cookie c : cookies){
					if(c.getName().equals(GOODS_PREFEX+goodsId)){
						cflag=true;
						int old_quantity=c.getValue()==null?0:Integer.parseInt(c.getValue());
						if(flag==1){
							quantity=old_quantity+quantity;
						}else if(flag==-1){
							quantity=old_quantity-quantity;
						}
						int maxAge=quantity<=0?0:60*60*1000;
						c.setValue(quantity+"");
						c.setMaxAge(maxAge);
						response.addCookie(c);
						break;
					}
				}
			}
			if(!cflag){
				Cookie cookie=new Cookie(GOODS_PREFEX+goodsId, quantity+"");
				cookie.setMaxAge(60*60*1000);
				response.addCookie(cookie);
			}
		}else{
			shoppingCartService.changeCartItem(Integer.parseInt(userId.toString()), goodsId, quantity, flag);
		}
		return "0";
	}
	
	/**
	 * 移除购物车里的商品
	 * @param request
	 * @param response
	 */
	@RequestMapping
	@ResponseBody
	public String removeGoodsInCart(int[] goodsIds,HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		Object userId=session.getAttribute("userId");
		//用户未登录
		if(userId==null){
			Cookie[] cookies=request.getCookies();
			if(cookies!=null){
				for(int goodsId : goodsIds){
					for(Cookie c : cookies){
						if(c.getName().equals(GOODS_PREFEX+goodsId)){
							c.setMaxAge(0);
							response.addCookie(c);
							break;
						}
					}
				}
			}
		}else{
			shoppingCartService.removeCartItems(Integer.parseInt(userId.toString()), goodsIds);
		}
		return "0";
	}
	
}
